package com.ldc.wandroid.ui.fragments;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ldc.common.mvvm.BaseMvvmFragment;
import com.ldc.common.utils.ViewModelUtils;
import com.ldc.wandroid.adapter.HomeArticleAdapter;
import com.ldc.wandroid.databinding.FragmentHomeBinding;
import com.ldc.wandroid.model.ArticleModel;
import com.ldc.wandroid.model.BannerModel;
import com.ldc.wandroid.viewmodel.HomeViewModel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;


public class HomeFragment extends BaseMvvmFragment<FragmentHomeBinding, HomeViewModel> {
    private int page = 0;
    private HomeArticleAdapter homeArticleAdapter;

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.getBanner().removeObserver(this::bannerData);
    }

    public static HomeFragment newInstance() {
        Bundle args = new Bundle();
        HomeFragment fragment = new HomeFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected FragmentHomeBinding initViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return FragmentHomeBinding.inflate(inflater, container, false);
    }


    @Override
    protected void main(Bundle savedInstanceState) {
        viewBinding.stateView.showLoadingView();
        // 初始化适配器
        initAdapter();
        // 加载数据
        viewModel.getBanner().observeForever(this::bannerData);
        viewModel.getArticle().observeForever(this::articleData);
        viewModel.loadBanner();
        viewModel.loadArticle(page);

    }

    private void initAdapter() {
        viewBinding.contentView.refreshView.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page += 1;
                viewModel.loadArticle(page);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 0;
                viewModel.loadArticle(page);
            }
        });
        homeArticleAdapter = new HomeArticleAdapter();
        viewBinding.contentView.recyclerView.setLayoutManager(new LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false));
        viewBinding.contentView.recyclerView.setHasFixedSize(true);
        viewBinding.contentView.recyclerView.setAdapter(homeArticleAdapter);
    }

    @Override
    protected HomeViewModel initViewModel() {
        return ViewModelUtils.getViewModel(this, HomeViewModel.class);
    }


    private void bannerData(List<BannerModel> bannerModels) {
    }

    private void articleData(ArticleModel dataBeans) {
        viewBinding.stateView.showContentView();
        viewBinding.contentView.refreshView.finishRefresh();
        viewBinding.contentView.refreshView.finishLoadMore();
        viewBinding.contentView.refreshView.setNoMoreData(dataBeans.getPageCount() == page);
        if (page == 0) {
            // 首次加载
            homeArticleAdapter.setNewData(dataBeans.getDatas());
        } else {
            homeArticleAdapter.addNewData(dataBeans.getDatas());
        }
    }
}
